
import UIKit
import StoreKit

class SettingViewController: UIViewController {
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        if #available(iOS 15.0, *) {
            let barAppearance = UINavigationBarAppearance.init()
            barAppearance.backgroundColor = .clear
            navigationController?.navigationBar.scrollEdgeAppearance = nil
            navigationController?.navigationBar.standardAppearance = barAppearance
        } else {
            navigationController?.navigationBar.barTintColor = .clear
            navigationController?.navigationBar.shadowImage = UIImage()
            navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
        }
        navigationController?.navigationBar.isTranslucent = true
        navigationController?.navigationBar.tintColor = .clear
    }
    
    let reportBtn = UIButton.init()
    let lickBtn = UIButton.init()
    let ruleBtn = UIButton.init()
    let clearBtn = UIButton.init()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view.
        
        let leftBtn = UIButton()
        leftBtn.setImage(UIImage(named: "back_icon"), for: .normal)
        leftBtn.frame = CGRect(x: 0, y: 0, width: 44, height: 44)
        navigationItem.leftBarButtonItem = UIBarButtonItem.init(customView: leftBtn)
        leftBtn.addTarget(self, action: #selector(goBack), for: .touchUpInside)
        
        let bgView = UIImageView.init(image: UIImage(named: "bg_icon"))
        bgView.frame = view.bounds
        bgView.isUserInteractionEnabled = true
        bgView.contentMode = .scaleAspectFill
        view.addSubview(bgView)
        
        let title = UILabel.init()
        title.text = "设置中心"
        title.font = .systemFont(ofSize: 30, weight: .semibold)
        title.textColor = .white
        view.addSubview(title)
        title.snp.makeConstraints { make in
            make.left.equalTo(20*DS.s)
            make.right.equalTo(-20*DS.s)
            make.top.equalTo(20*DS.s+kNavigationH)
        }
        
        reportBtn.backgroundColor = .hexString("#5d5a5a")
        reportBtn.setTitle("建议意见反馈", for: .normal)
        reportBtn.setTitleColor(.white, for: .normal)
        reportBtn.titleLabel?.font = .systemFont(ofSize: 30, weight: .semibold)
        reportBtn.setImage(UIImage(named: "report"), for: .normal)
        reportBtn.imageEdgeInsets = UIEdgeInsets(top: 0, left: -10, bottom: 0, right: 0)
        reportBtn.titleEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: -10)
        view.addSubview(reportBtn)
        reportBtn.snp.makeConstraints { make in
            make.left.right.equalTo(title)
            make.top.equalTo(title.snp.bottom).offset(30*DS.s)
            make.height.equalTo((KScreenWidth-40*DS.s)/335*128)
        }
        reportBtn.addTarget(self, action: #selector(btnClick(sender: )), for: .touchUpInside)
        reportBtn.tag = 1
        
        lickBtn.backgroundColor = .hexString("#5d5a5a")
        lickBtn.setTitle("好评鼓励", for: .normal)
        lickBtn.setTitleColor(.white, for: .normal)
        lickBtn.titleLabel?.font = .systemFont(ofSize: 24, weight: .semibold)
        lickBtn.setImage(UIImage(named: "like"), for: .normal)
        lickBtn.centerImageAndTitle()
        view.addSubview(lickBtn)
        lickBtn.snp.makeConstraints { make in
            make.left.equalTo(title)
            make.top.equalTo(reportBtn.snp.bottom).offset(30*DS.s)
            make.width.equalTo((KScreenWidth-60*DS.s)/2)
            make.height.equalTo((KScreenWidth-60*DS.s)/2/79*68)
        }
        lickBtn.addTarget(self, action: #selector(btnClick(sender: )), for: .touchUpInside)
        lickBtn.tag = 2
        
        ruleBtn.tag = 3
        ruleBtn.backgroundColor = .hexString("#5d5a5a")
        ruleBtn.setTitle("隐私协议", for: .normal)
        ruleBtn.setTitleColor(.white, for: .normal)
        ruleBtn.titleLabel?.font = .systemFont(ofSize: 24, weight: .semibold)
        ruleBtn.setImage(UIImage(named: "rule"), for: .normal)
        ruleBtn.centerImageAndTitle()
        view.addSubview(ruleBtn)
        ruleBtn.snp.makeConstraints { make in
            make.right.equalTo(title)
            make.top.equalTo(reportBtn.snp.bottom).offset(30*DS.s)
            make.width.equalTo((KScreenWidth-60*DS.s)/2)
            make.height.equalTo((KScreenWidth-60*DS.s)/2/79*68)
        }
        ruleBtn.addTarget(self, action: #selector(btnClick(sender: )), for: .touchUpInside)
        
        clearBtn.backgroundColor = .hexString("#5d5a5a")
        clearBtn.setTitle("清除缓存", for: .normal)
        clearBtn.setTitleColor(.white, for: .normal)
        clearBtn.titleLabel?.font = .systemFont(ofSize: 24, weight: .semibold)
        clearBtn.setImage(UIImage(named: "clear"), for: .normal)
        clearBtn.imageEdgeInsets = UIEdgeInsets(top: 0, left: -10, bottom: 0, right: 0)
        clearBtn.titleEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: -10)
        view.addSubview(clearBtn)
        clearBtn.snp.makeConstraints { make in
            make.left.right.equalTo(title)
            make.top.equalTo(ruleBtn.snp.bottom).offset(30*DS.s)
            make.height.equalTo((KScreenWidth-40*DS.s)/335*128)
        }
        clearBtn.addTarget(self, action: #selector(btnClick(sender: )), for: .touchUpInside)
        clearBtn.tag = 4
        
        reportBtn.layer.cornerRadius = 10
        reportBtn.layer.masksToBounds = true
        ruleBtn.layer.cornerRadius = 10
        ruleBtn.layer.masksToBounds = true
        lickBtn.layer.cornerRadius = 10
        lickBtn.layer.masksToBounds = true
        clearBtn.layer.cornerRadius = 10
        clearBtn.layer.masksToBounds = true
    }
    
    @objc func btnClick(sender:UIButton){
        
        let tag = sender.tag
        switch tag {
        case 1:
            
            let vc = SettingReportViewController()
            self.navigationController?.pushViewController(vc, animated: true)
        case 2:
            SKStoreReviewController.requestReview()
            
        case 3:
            if let url = URL(string: "https://gitee.com/majujike/user_kown/raw/master/raw") {
                UIApplication.shared.open(url, options: [:], completionHandler: nil)
            }
        default:
            self.view.makeToast("缓存已清除")
        }
    }
    
    @objc func goBack(){
        
        navigationController?.popViewController(animated: true)
    }
}

extension UIButton {
    func centerImageAndTitle() {
        guard let imageView = self.imageView, let titleLabel = self.titleLabel else {
            return
        }
        
        self.contentHorizontalAlignment = .center
        self.contentVerticalAlignment = .center
        
        let spacing: CGFloat = 8.0 // 调整图片和文字之间的间距
        
        let imageSize = imageView.intrinsicContentSize
        let titleSize = titleLabel.intrinsicContentSize
        let totalHeight = imageSize.height + spacing + titleSize.height
        
        self.imageEdgeInsets = UIEdgeInsets(top: -(totalHeight - imageSize.height),
                                            left: 0,
                                            bottom: 0,
                                            right: -titleSize.width)
        
        self.titleEdgeInsets = UIEdgeInsets(top: 0,
                                            left: -imageSize.width,
                                            bottom: -(totalHeight - titleSize.height),
                                            right: 0)
    }
}

